package com.iversonx.example_1.controller.v2.impl;

import com.iversonx.example_1.controller.AwardReq;
import com.iversonx.example_1.controller.v2.PrizeService;
import com.iversonx.example_1.service.goods.DeliverReq;
import com.iversonx.example_1.service.goods.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GoodsPrizeService implements PrizeService {
    private final Logger logger = LoggerFactory.getLogger(GoodsPrizeService.class);

    @Autowired
    private GoodsService goodsService;

    @Override
    public boolean support(Integer awardType) {
        return awardType == 2;
    }

    @Override
    public void sendCommodity(AwardReq req) throws Exception {
        logger.info("请求参数[实物商品] => {}", req);
        DeliverReq deliverReq = new DeliverReq();
        deliverReq.setUserName(queryUserName(req.getUId()));
        deliverReq.setUserPhone(queryUserPhoneNumber(req.getUId()));
        deliverReq.setSku(req.getAwardNumber());
        deliverReq.setOrderId(req.getBizId());
        deliverReq.setConsigneeUserName(req.getExtMap().get("consigneeUserName"));
        deliverReq.setConsigneeUserPhone(req.getExtMap().get("consigneeUserPhone"));
        deliverReq.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));
        Boolean isSuccess = goodsService.deliverGoods(deliverReq);
        logger.info("测试结果[实物商品]：{}", isSuccess);

        if (!isSuccess) throw new RuntimeException("实物商品发放失败");
    }

    private String queryUserName(String uId) {
        return "花花";
    }

    private String queryUserPhoneNumber(String uId) {
        return "15200101232";
    }
}
